package com;

import java.util.Random;

/**
 * Created by bruce on 2016/5/22.
 */
public class SerialNumber {

    public static String PAY_NUM = "PN";

    public static String getRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getSN(String type, Integer storeId) {
        long timestamp = System.currentTimeMillis();
        return type + convert((long)storeId) + convert(timestamp);
    }

    private static String convert(Long val) {

        StringBuilder builder = new StringBuilder();

        int right = 0;

        do {
            right = (int) (val % 36);
            val = (long) Math.floor((double)val / 36);
            builder.insert(0, getString(right));
        } while(val >0);

        int len = builder.length();

        while(len++ < 4) {
            builder.insert(0, '0');
        }

        return builder.toString();
    }

    private static String getString(Integer val) {
        if(val < 10)
            return val.toString();
        else
            return String.valueOf(Character.toChars(val + 55));
    }

}
